<template>
  <el-container class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="200px" class="sidebar">
      <div class="logo">
        <h3>🎮 心理游戏后台</h3>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        background-color="#1e3a8a"
        text-color="#e5e7eb"
        active-text-color="#ffffff"
        router
        :collapse="false"
      >
        <el-menu-item index="/admin/games">
          <el-icon><Game /></el-icon>
          <span>游戏库管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/teachers">
          <el-icon><UserFilled /></el-icon>
          <span>导师库管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container>
      <!-- 顶部导航 -->
      <el-header class="header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin/games' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-button type="danger" size="small" @click="logout">
            <el-icon><SwitchButton /></el-icon>
            退出登录
          </el-button>
        </div>
      </el-header>

      <!-- 内容主体 -->
      <el-main class="main-content">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Game, UserFilled, SwitchButton } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const activeMenu = computed(() => route.path)

const currentTitle = computed(() => {
  switch (route.name) {
    case 'GameManagement':
      return '游戏库管理'
    case 'TeacherManagement':
      return '导师库管理'
    default:
      return '管理后台'
  }
})

const logout = () => {
  localStorage.removeItem('token')
  ElMessage.success('已退出登录')
  router.push('/login')
}

onMounted(() => {
  const token = localStorage.getItem('token')
  if (!token) {
    ElMessage.warning('请先登录')
    router.push('/login')
  }
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.sidebar {
  background-color: #1e3a8a;
  color: white;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #1e40af;
  color: white;
  font-weight: bold;
}

.el-menu-vertical {
  flex: 1;
  border-right: none;
}

.el-menu-item {
  display: flex;
  align-items: center;
}

.el-menu-item:hover {
  background-color: #1e40af !important;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f9fafb;
  border-bottom: 1px solid #e5e7eb;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.header-left {
  font-size: 16px;
  color: #374151;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.main-content {
  background-color: #f3f4f6;
  padding: 20px;
  overflow-y: auto;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  font-weight: bold;
  color: #1f2937;
}
</style>
